#pragma once
#include "uart/ProtocolSender.h"
#include <base/base.h>
#include "utils/GpioHelper.h"
#include "STC_Download.h"
#include "uart/UartContext.h"
/*
*此文件由GUI工具生成
*文件功能：用于处理用户的逻辑相应代码
*功能说明：
*========================onButtonClick_XXXX
当页面中的按键按下后系统会调用对应的函数，XXX代表GUI工具里面的[ID值]名称，
如Button1,当返回值为false的时候系统将不再处理这个按键，返回true的时候系统将会继续处理此按键。比如SYS_BACK.
*========================onSlideWindowItemClick_XXXX(int index)
当页面中存在滑动窗口并且用户点击了滑动窗口的图标后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称，
如slideWindow1;index 代表按下图标的偏移值
*========================onSeekBarChange_XXXX(int progress)
当页面中存在滑动条并且用户改变了进度后系统会调用此函数,XXX代表GUI工具里面的[ID值]名称，
如SeekBar1;progress 代表当前的进度值
*========================ogetListItemCount_XXXX()
当页面中存在滑动列表的时候，更新的时候系统会调用此接口获取列表的总数目,XXX代表GUI工具里面的[ID值]名称，
如List1;返回值为当前列表的总条数
*========================oobtainListItemData_XXXX(ZKListView::ZKListItem *pListItem, int index)
 当页面中存在滑动列表的时候，更新的时候系统会调用此接口获取列表当前条目下的内容信息,XXX代表GUI工具里面的[ID值]名称，
如List1;pListItem 是贴图中的单条目对象，index是列表总目的偏移量。具体见函数说明
*========================常用接口===============
*LOGD(...)  打印调试信息的接口
*mTextXXXPtr->setText("****") 在控件TextXXX上显示文字****
*mButton1Ptr->setSelected(true); 将控件mButton1设置为选中模式，图片会切换成选中图片，按钮文字会切换为选中后的颜色
*mSeekBarPtr->setProgress(12) 在控件mSeekBar上将进度调整到12
*mListView1Ptr->refreshListView() 让mListView1 重新刷新，当列表数据变化后调用
*mDashbroadView1Ptr->setTargetAngle(120) 在控件mDashbroadView1上指针显示角度调整到120度
*
* 在Eclipse编辑器中  使用 “alt + /”  快捷键可以打开智能提示
*/
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "manager/ConfigManager.h"

// SV50PD
#define SV50PD_A0 "A0"
#define SV50PD_A1 "A1"
#define SV50PD_A2 "A2"
#define SV50PD_A3 "A3"
#define SV50PD_A4 "A4"
#define SV50PD_A5 "A5"
#define SV50PD_A6 "A6"
#define SV50PD_A7 "A7"
#define SV50PD_A8 "A8"

#define SRC_PATH "/mnt/extsd"

const char *Ox0F50_cfg = "/mnt/extsd/Ox0F50_cfg.hex"; // 文件保存的路径
const char *Ox0008_cfg = "/mnt/extsd/Ox0008_cfg.hex"; // 文件保存的路径
const char *Ox000C_cfg = "/mnt/extsd/Ox000C_cfg.hex"; // 文件保存的路径
const char *Ox0101_cfg = "/mnt/extsd/Ox0101_cfg.hex"; // 文件保存的路径
const char *Ox0303_cfg = "/mnt/extsd/Ox0303_cfg.hex"; // 文件保存的路径
const char *Ox3304_cfg = "/mnt/extsd/Ox3304_cfg.hex"; // 文件保存的路径
const char *Ox3304_txt = "/mnt/extsd/Ox3304_txt.txt"; // 文件保存的路径
string Download_Path;                                 // 下载文件的路径
typedef struct
{
   char path[256];
   int type;
   char name[256];
} FileInfo;

typedef struct
{
   uint8_t buffer[128000];
   uint16_t sum_length;
   uint32_t checksum;
   uint32_t addr;
} BIN_ROM;

uint16_t delay2mS; // 延时2ms
uint16_t delay5mS; // 延时1s

uint16_t delay2mS_en; // 延时2ms
uint16_t delay5mS_en; // 延时1s

void delay_5ms(uint16_t ms)
{
   if (delay2mS_en)
   {
      /* code */
      delay2mS_en = 0;
      //      GpioHelper::output(SV50PD_A7, 1);
   }
   else
   {
      /* code */
      delay2mS_en = 1;
      //      GpioHelper::output(SV50PD_A7, 0);
   }
}

BIN_ROM bin_rom;

static vector<FileInfo> fileList;
static vector<FileInfo> curPathFile;
static string curPath = SRC_PATH;
static int sTotalFileNum = 0;

const char *filename = "/mnt/extsd/STC8G_H_UART1_Demo.hex";
const char *HEX_16 = "hex";

BYTE Download_bit;
BYTE Download_OK;
typedef struct
{
   uint8_t buffer[256];
   uint8_t length;
} A;

// 将ASCII码转换为十进制数
uint8_t ASCII_To_Sum(uint8_t ASCII)
{
   // 如果ASCII码小于30，则返回0x0
   if (ASCII >= 0x30 && ASCII <= 0x39)
   {
      return ASCII - 0x30;
   }
   // 如果ASCII码在40和46之间，则返回ASCII码减去37
   else if (ASCII >= 0x41 && ASCII <= 0x46)
   {
      return ASCII - 0x37;
   }
   else if (ASCII >= 0x61 && ASCII <= 0x66)
   {
      return ASCII - 0x57;
   }
   // 如果ASCII码不在上述范围内，则返回0x0
   return 0x0;
}

// 将ASCII码转换为十进制数
uint8_t Sum_To_ASCII(uint8_t ASCII)
{
   if (ASCII >= 0 && ASCII <= 9)
   {
      return ASCII + 0x30;
   }
   else if (ASCII >= 10 && ASCII <= 15)
   {
      return ASCII + 0x37;
   }
   return 0;
}

// 计算校验和
uint8_t calculate_checksum(const char *line, int length)
{
   uint8_t sum = 0;
   for (int i = 7; i < length; i += 2)
   {
      uint8_t byte;
      sscanf(line + i, "%2hhx", &byte);
      sum += byte;
   }
   return sum;
}
//: 00 0000 01 FF
// 将 HEX 文件解析为 BIN 文件
void hex_to_buffer(const char *hex_filename)
{
   uint8_t line_len = 0;        // 行数据长度
   uint16_t line_add = 0;       // 行数据地址
   uint8_t line_type = 0;       // 行数据类型
   uint16_t line_data = 0;      // 行数据
   uint8_t line_check = 0;      // 行校验和
   uint8_t line_user_check = 0; // 用户计算的行校验和

   memset(bin_rom.buffer, 0xFF, 128000); // 初始化缓冲区全为0xFF
   bin_rom.sum_length = 0;

   std::string content = base::readFile(hex_filename); // 读取文件的所有内容，保存在content中
   for (size_t i = 0; i < content.size(); ++i)
   {
      if (content.data()[i] == ':')
      { /*判断行数据头*/

         // 计算 行数据长度
         line_len = ASCII_To_Sum(content.data()[i + 1]) << 4 | ASCII_To_Sum(content.data()[i + 2]);

         // 计算 行数据地址
         line_add = ASCII_To_Sum(content.data()[i + 3]) << 12 | ASCII_To_Sum(content.data()[i + 4]) << 8 | ASCII_To_Sum(content.data()[i + 5]) << 4 | ASCII_To_Sum(content.data()[i + 6]);

         // 计算 行数据类型
         line_type = ASCII_To_Sum(content.data()[i + 7]) << 4 | ASCII_To_Sum(content.data()[i + 8]);

         // 计算 行校验和
         line_check = ASCII_To_Sum(content.data()[(line_len - 1) * 2 + i + 11]) << 4 | ASCII_To_Sum(content.data()[(line_len - 1) * 2 + i + 12]);

         if (line_type == 0x00)
         { /*判断本行数据为数据类型*/
            if (bin_rom.sum_length < (line_len + line_add))
            {
               bin_rom.sum_length = line_len + line_add;
            }
            for (size_t y = 0; y < line_len; y++)
            {
               bin_rom.buffer[line_add++] = ASCII_To_Sum(content.data()[y * 2 + +i + 9]) << 4 | ASCII_To_Sum(content.data()[y * 2 + i + 10]);
            }
         }
         else if (line_type == 0x01)
         { /*判断本行数据为结束行*/
            ;
         }
      }
   }
   bin_rom.checksum = 0;
   for (size_t i = 0; i < bin_rom.sum_length; i++)
   {
      bin_rom.checksum += bin_rom.buffer[i];
   }
   //   LOGD("此HEX文件数据长度为%02d(0x%02X)字节,校验和为%02d(0x%02X)", bin_rom.sum_length, bin_rom.sum_length, bin_rom.checksum, bin_rom.checksum);
   char str[12];
   sprintf(str, "%02X", bin_rom.sum_length);
   mTextLenPtr->setText(str);
   sprintf(str, "%02X", bin_rom.checksum);
   mTextSumPtr->setText(str);
}

// 将 cfg 文件解析为 buffer区
void cfg_to_buffer(const char *hex_filename, A *cfg)
{
   std::string content = base::readFile(hex_filename); // 读取文件的所有内容，保存在content中
   cfg->length = content.size() / 2;
   for (size_t i = 0; i < cfg->length; i++)
   {
      cfg->buffer[i] = ASCII_To_Sum(content.data()[i * 2]) << 4 | ASCII_To_Sum(content.data()[i * 2 + 1]);
   }
}
// 将 buffer区  解析为 cfg文件
void buffer_to_cfg(const char *hex_filename, A *cfg)
{
   std::string content;
   for (size_t i = 0; i < cfg->length; i++)
   {
      content += Sum_To_ASCII((cfg->buffer[i] >> 4) & 0x0f);
      content += Sum_To_ASCII(cfg->buffer[i] & 0x0f);
   }
   base::writeFile(hex_filename, content.c_str());
}

void printf_debug()
{
   //   std::string hex_filename = "/mnt/extsd/UART1.hex";
   //   std::string SD_path_name = "/mnt/extsd";
   //   std::string my_hex_filename = "UART1.hex";
   //   std::string my_hex_path = "";
   //   my_hex_path += SD_path_name;
   //   my_hex_path += "/";
   //   my_hex_path += my_hex_filename;
   //   hex_to_buffer(my_hex_path.c_str());
}

void scanDirectories(const std::string &path)
{
   DIR *dir = opendir(path.c_str());
   if (dir)
   {
      struct dirent *entry;
      while ((entry = readdir(dir)) != nullptr)
      {
         if (entry->d_type == DT_DIR)
         {
            if (std::string(entry->d_name) != "." && std::string(entry->d_name) != "..")
            {
               std::string subDir = path + "/" + entry->d_name;
               std::cout << subDir << std::endl;
               scanDirectories(subDir); // 递归扫描子目录
            }
         }
      }
      closedir(dir);
   }
}

/**
 * 扫描文件
 * path:扫描路径
 * fileType:指令扫描文件类型，为空时表示不指定文件类型
 */
static bool ScanFile(const char *path, const char *fileType)
{
   bool isHaveFile = false;
   struct dirent *d = NULL;
   DIR *pDir;

   pDir = opendir(path);
   if (pDir == NULL)
   {
      // 被当作目录，但是执行opendir后发现又不是目录，比如软链接就会发生这样的情况。
      return 0;
   }
   while (NULL != (d = readdir(pDir)))
   {
      FileInfo info;

      if (d->d_type == DT_DIR)
      {
         if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
         {
            continue;
         }
         // directory
         string tempPath(path);
         tempPath += "/";
         tempPath += d->d_name;
         string fullDirPath = tempPath;
         if (curPath == path)
         {
            memset(info.path, 0, 256);
            strcpy(info.path, path);
            info.type = DT_DIR;
            memset(info.name, 0, 256);
            strcpy(info.name, d->d_name);
            fileList.push_back(info);
         }
         ScanFile(fullDirPath.c_str(), fileType);
      }
   }

   pDir = opendir(path);
   if (pDir == NULL)
   {
      // 被当作目录，但是执行opendir后发现又不是目录，比如软链接就会发生这样的情况。
      return 0;
   }
   while (NULL != (d = readdir(pDir)))
   {
      FileInfo info;

      if (d->d_type == DT_REG)
      {
         // /*file*/
         string name = d->d_name;
         unsigned int pos = name.rfind(".");
         if (pos != string::npos)
         {
            name = name.substr(pos + 1, name.length());
            if (strcmp(fileType, name.c_str()) == 0)
            {
               if (curPath == path)
               {
                  memset(info.path, 0, 256);
                  strcpy(info.path, path);
                  info.type = DT_REG;
                  memset(info.name, 0, 256);
                  strcpy(info.name, d->d_name);
                  fileList.push_back(info);
               }
               isHaveFile = true;
            }
         }
      }
   }
   closedir(pDir);
   return isHaveFile;
}

/**
 * 计算要显示的文件数量
 */
static int getFileNum()
{
   sTotalFileNum = 0;
   curPathFile.clear();
   std::vector<FileInfo>::iterator it = fileList.begin();
   for (; it != fileList.end(); it++)
   {
      if (((strcmp(it->path, curPath.c_str()) == 0)))
      {
         sTotalFileNum++;
         curPathFile.push_back(*it);
      }
   }
   return sTotalFileNum;
}

void Log_DisplayLn(const char *text)
{
   string in_buf = text;
   string out_buf = mdebugPtr->getText();
   in_buf += "\n";
   in_buf += out_buf;
   if (in_buf.length() > 1195)
   {
      in_buf = in_buf.substr(0, 1195);
   }
   mdebugPtr->setText(in_buf);
}

/**
 * 注册定时器
 * 填充数组用于注册定时器
 * 注意：id不能重复
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
    //{0,  6000}, //定时器id=0, 时间间隔6秒
    //  {1, 150},
    {2, 80},
    {3, 80},
};

/**
 * 当界面构造时触发
 */
static void onUI_init()
{
   // Tips :添加 UI初始化的显示代码到这里,如:mText1Ptr->setText("123");
}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr)
{
   if (intentPtr != NULL)
   {
      // TODO
   }
}

/*
 * 当界面显示时触发
 */
static void onUI_show()
{
   mCurPathButtonPtr->setText(SRC_PATH);
   fileList.clear();
   ScanFile(SRC_PATH, HEX_16);
   getFileNum();
   GpioHelper::output(SV50PD_A0, 0);
   Download_OK = 0; // 下载完成标志
}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide()
{
}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit()
{
}

BYTE arg;
uint32_t offset;
uint32_t cnt;
uint32_t addr;

BYTE debug_buffer[256];

uint32_t frequency;

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data)
{
   BYTE y = 5;
   A test_in;
   //   LOGD("date %02x", data.data[y + 0]);
   switch (data.data[y + 0])
   {
   case 0x50:
      Download_bit = 8;
      arg = data.data[y + 4];
      Log_DisplayLn("握手完成");
      break;

   case 0x01:
      Download_bit = 2;
      break;

   case 0x05:
      Download_bit = 3;
      Log_DisplayLn("准备编程完成，准备擦除");
      break;

   case 0x03:
      Log_DisplayLn("擦除完成，开始烧录");
      Download_bit = 4;
      break;

   case 0x02:
      Download_bit = 5;
      if (Download_OK == 1)
      {
         Download_bit = 6;
      }

      break;

   case 0x04:
      Download_bit = 0;
      Log_DisplayLn("调节频率成功");
      Log_DisplayLn("烧录成功");
      // debug_buffer[255] = data.len;
      // test_in.length = data.len;
      // for (int var = 0; var < data.len; ++var)
      // {
      //    LOGD("date4[%d] = %02x", var, data.data[var]);
      //    debug_buffer[var] = data.data[var];
      // }
      // for (size_t i = 0; i < test_in.length; i++)
      // {
      //    test_in.buffer[i] = data.data[i];
      // }
      // buffer_to_cfg(Ox3304_cfg, &test_in);
      break;

   default:
      // for (int var = 0; var < data.len - y; ++var)
      // {
      //    LOGD("onProtocolDataUpdate %02x", data.data[var + y]);
      // }
      break;
   }
}

A test_in0;

char download_str[20];
uint16_t download_ing; // 下载中标志
float download_f;      // 下载中标志
/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool
onUI_Timer(int id)
{
   switch (id)
   {
   case 1:
      //      send_7F();
      break;

   case 2:
      switch (Download_bit)
      {
      case 1:
         // 握手 命令
         send_7F();
         break;

      case 8:
         // 握手 命令
         Download_bit = 0;
         TxBuffer[0] = 0x01;
         TxBuffer[1] = 0x00; // arg

         TxBuffer[2] = 0x00; // 19200
         TxBuffer[3] = 0xFE;
         TxBuffer[4] = 0xC7;

         // TxBuffer[2] = 0x00;//115200
         // TxBuffer[3] = 0xFF;
         // TxBuffer[4] = 0xCC;

         TxBuffer[5] = 0x00;
         TxBuffer[6] = 0x00;
         TxBuffer[7] = 0x98;
         CommSend(8);
         break;

      case 2:
         Download_bit = 0;
         // 发送 准备编程 命令
         TxBuffer[0] = 0x05;
         TxBuffer[1] = 0x00;
         TxBuffer[2] = 0x00;
         TxBuffer[3] = 0x5a;
         TxBuffer[4] = 0xa5;
         CommSend(5);
         Log_DisplayLn("开始配置");
         break;

      case 3: // 发送 擦除Flash 命令
         Download_bit = 0;
         TxBuffer[0] = 0x03;
         TxBuffer[1] = 0x00;
         TxBuffer[2] = 0x00;
         TxBuffer[3] = 0x5a;
         TxBuffer[4] = 0xa5;
         CommSend(5);
         Log_DisplayLn("开始擦除");
         break;

      case 4: // 首次写用户代码
         Log_DisplayLn("开始烧录，等待完成");
         Download_bit = 0;
         bin_rom.addr = 0;
         download_ing = 1;
         addr = 0;
         TxBuffer[0] = 0x22;
         TxBuffer[3] = 0x5a;
         TxBuffer[4] = 0xa5;
         offset = 5;
         if (addr < bin_rom.sum_length)
         {
            TxBuffer[1] = HI_BYTE(addr);
            TxBuffer[2] = LO_BYTE(addr);
            cnt = 0;
            while (addr < bin_rom.sum_length)
            {
               TxBuffer[cnt + offset] = bin_rom.buffer[addr];
               addr++;
               cnt++;
               if (cnt >= 128)
                  break;
            }
            CommSend((uint8_t)(cnt + offset));
         }
         else
         {
            LOGD("bin_rom.sum_length d%", bin_rom.sum_length)
         }

         break;

      case 5: // 持续写用户代码
         Download_bit = 0;
         TxBuffer[0] = 0x02;
         TxBuffer[3] = 0x5a;
         TxBuffer[4] = 0xa5;
         offset = 5;
         if (addr < bin_rom.sum_length)
         {
            TxBuffer[1] = HI_BYTE(addr);
            TxBuffer[2] = LO_BYTE(addr);
            cnt = 0;
            while (addr < bin_rom.sum_length)
            {
               TxBuffer[cnt + offset] = bin_rom.buffer[addr];
               addr++;
               cnt++;
               if (cnt >= 128)
                  break;
            }

            download_f = (float)addr / bin_rom.sum_length;
            // download_ing = download_f * 100;
            if ((download_f * 100) > download_ing)
            {
               download_ing = download_ing + 10;
               sprintf(download_str, "烧录中 %d ", download_ing - 1);
               Log_DisplayLn(download_str);
            }

            for (size_t i = 10; i > 1; i--)
            {
               if (cnt < bin_rom.sum_length / i)
                  break;
            }

            if (cnt < 128)
            {
               Log_DisplayLn("上传完成");
               Download_OK = 1;
            }

            CommSend((uint8_t)(cnt + offset));
         }
         break;

      case 6: // 烧录完成
         Download_bit = 0;
         Log_DisplayLn("调节频率");
         cfg_to_buffer(Ox3304_txt, &test_in0);
         send_UART(test_in0.buffer, test_in0.length);
         // frequency = 0;
         // for (int var = 0; var < 38; ++var)
         // {
         //    frequency = frequency + debug_buffer[var];
         //    //            LOGD("Data[%d] = %x", var, debug_buffer[var]);
         // }
         // debug_buffer[0] = frequency;
         // debug_buffer[1] = frequency >> 8;
         // for (int var = 0; var < debug_buffer[4]; ++var)
         // {
         //    frequency = frequency + debug_buffer[var];
         //    //                     LOGD("Data[%d] = %x", var, debug_buffer[var]);
         // }
         // //         LOGD("%x", debug_buffer[0]);
         // //         LOGD("%x", debug_buffer[1]);
         // if (debug_buffer[255] > 20)
         // {
         //    for (int var = 0; var < 7; ++var)
         //    {
         //       debug_buffer[var] = debug_buffer[var + 34];
         //    }
         // }
         // send_UART(debug_buffer, 7);
         // frequency = debug_buffer[0] << 24 | debug_buffer[1] << 16 | debug_buffer[2] << 8 | debug_buffer[3];
         // LOGD("%d", frequency);
         // mTextView1Ptr->setText((uint16_t)(frequency / 1000));
         break;

      default:
         break;
      }
      break;

   case 3:
      // send_7F();
      break;
   default:
      break;
   }
   return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool onmainActivityTouchEvent(const MotionEvent &ev)
{
   switch (ev.mActionStatus)
   {
   case MotionEvent::E_ACTION_DOWN: // 触摸按下
      // LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
      break;
   case MotionEvent::E_ACTION_MOVE: // 触摸滑动
      break;
   case MotionEvent::E_ACTION_UP: // 触摸抬起
      break;
   default:
      break;
   }
   return false;
}
static int getListItemCount_FileListview(const ZKListView *pListView)
{
   // LOGD("getListItemCount_FileListview !\n");
   return sTotalFileNum;
}

static void obtainListItemData_FileListview(ZKListView *pListView, ZKListView::ZKListItem *pListItem, int index)
{
   // LOGD(" obtainListItemData_ FileListview  !!!\n");
   // 获取列表子项，并设置要显示的内容
   ZKListView::ZKListSubItem *pSubIcon = pListItem->findSubItemByID(ID_MAIN_IconSubItem);
   ZKListView::ZKListSubItem *pSubName = pListItem->findSubItemByID(ID_MAIN_FileNameSubItem);
   //	pSubIcon->setBackgroundPic((curPathFile.at(index).type == DT_REG) ? CONFIGMANAGER->getResFilePath("file.png").c_str() : CONFIGMANAGER->getResFilePath("folder.png").c_str());
   pSubName->setText(curPathFile.at(index).name);
}

/**
 * 点击按钮返回上一层目录
 */
static void onListItemClick_FileListview(ZKListView *pListView, int index, int id)
{

   // LOGD(" onListItemClick_ FileListview  !!!\n");
   if (curPathFile.at(index).type == DT_DIR)
   {
      curPath += '/';
      curPath += curPathFile.at(index).name;
      mCurPathButtonPtr->setText(curPath.c_str());
      fileList.clear();
      ScanFile(curPath.c_str(), HEX_16);
      getFileNum();
      mFileListviewPtr->refreshListView();
      mFileListviewPtr->setSelection(0);
   }
   else
   {
      Download_Path = curPath + '/' + curPathFile.at(index).name;
      hex_to_buffer(Download_Path.c_str());
      mCurPathButtonPtr->setText(Download_Path.c_str());
      // LOGD("第%s字节", Download_Path.c_str());
   }

   // if (index == 1)
   // {
   //    // LOGD("第%02d字节", index);
   // }
}

static bool onButtonClick_CurPathButton(ZKButton *pButton)
{
   // LOGD(" ButtonClick CurPathButton !!!\n");
   // if (strcmp(curPath.c_str(), SRC_PATH))
   // {
   //    unsigned int pos = curPath.rfind('/');
   //    if (pos != string::npos)
   //    {
   //       curPath = curPath.substr(0, pos);
   //    }
   //    fileList.clear();
   //    ScanFile(curPath.c_str(), HEX_16); // HEX_16
   //    pButton->setText(curPath.c_str());
   //    getFileNum();

   //    mFileListviewPtr->refreshListView();
   //    mFileListviewPtr->setSelection(0);
   // }
   return false;
}
static void onEditTextChanged_FileTypeEdittext(const std::string &text)
{
   // LOGD(" onEditTextChanged_ FileTypeEdittext %s !!!\n", text.c_str());
   fileList.clear();
   ScanFile(SRC_PATH, HEX_16);
   getFileNum();
}

static bool onButtonClick_Button1(ZKButton *pButton)
{
   // LOGD(" ButtonClick Button1 !!!\n");
   // printf_debug();
   return false;
}
static bool onButtonClick_Button2(ZKButton *pButton)
{
   // LOGD(" ButtonClick Button2 !!!\n");
   // std::string content = ":0123456789abcedf";
   // // 将读到的每一个字节以16进制输出到日志
   // for (size_t i = 0; i < content.size(); ++i)
   // {
   //    //      LOGD("第%02d字节=0x%02X %02X %c", i, ASCII_To_Sum(content.data()[i]), content.data()[i], content.data()[i]);
   //    //      LOGD("第%02d字节=0x%02X %c", i, content.data()[i], content.data()[i]);
   // }
   hex_to_buffer(Download_Path.c_str());
   return false;
}
static bool onButtonClick_Button3(ZKButton *pButton)
{
   LOGD(" ButtonClick Button3 !!!\n");
   // A6 io口输出高电平
   GpioHelper::output(SV50PD_A0, 0);
   Download_bit = 1;
   return false;
}
static bool onButtonClick_Button4(ZKButton *pButton)
{
   LOGD(" ButtonClick Button4 !!!\n");
   // A6 io口输出高电平
   GpioHelper::output(SV50PD_A0, 1);
   Download_OK = 0;
   //   Download_bit = 1;
   return false;
}
static bool onButtonClick_Button5(ZKButton *pButton)
{
   LOGD(" ButtonClick Button5 !!!\n");
   A test_in;
   //   BYTE pdat[5];
   //   Download(pdat, 25); // 下载文件
   cfg_to_buffer(Ox3304_cfg, &test_in);
   for (int var = 0; var < test_in.length; ++var)
   {
      LOGD("date4[%d] = %02x", var, test_in.buffer[var]);
   }

   return false;
}
static bool onButtonClick_Button6(ZKButton *pButton)
{
   LOGD(" ButtonClick Button6 !!!\n");
   A test_in;
   //      cfg_to_buffer(Ox3304_cfg, &test_in);
   cfg_to_buffer(Ox3304_txt, &test_in);
   for (int var = 0; var < test_in.length; ++var)
   {
      LOGD("date4[%d] = %02x", var, test_in.buffer[var]);
   }
   //   buffer_to_cfg(Ox3304_txt, &test_in);
   return false;
}
static bool onButtonClick_Button7(ZKButton *pButton)
{
   LOGD(" ButtonClick Button7 !!!\n");
   if (strcmp(curPath.c_str(), SRC_PATH))
   {
      unsigned int pos = curPath.rfind('/');
      if (pos != string::npos)
      {
         curPath = curPath.substr(0, pos);
      }
      fileList.clear();
      ScanFile(curPath.c_str(), HEX_16); // HEX_16
      mCurPathButtonPtr->setText(curPath.c_str());
      getFileNum();

      mFileListviewPtr->refreshListView();
      mFileListviewPtr->setSelection(0);
   }
   return false;
}
